package com.guldan.core.jackson;

/**
 * Created by Silas.
 * Date: 2016/10/1
 * Time: 22:03
 */
import com.guldan.core.jackson.UnitSuffixSerialize;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.SerializerProvider;
import java.io.IOException;
import java.math.BigDecimal;

public class SignedMoneySerialize extends UnitSuffixSerialize {
    public SignedMoneySerialize(String prefix, String suffix) {
        this.prefix = prefix;
        this.suffix = suffix;
    }

    public void serialize(Number value, JsonGenerator gen, SerializerProvider serializers) throws IOException, JsonProcessingException {
        String valText = null;
        BigDecimal decimalValue = BigDecimal.valueOf(value.doubleValue());
        if(value != null && !decimalValue.equals(BigDecimal.ZERO)) {
            String prefix = "";
            if(decimalValue.compareTo(BigDecimal.ZERO) > 0) {
                prefix = "+";
            }

            valText = prefix + decimalValue.setScale(1, 3);
        } else {
            valText = "0.0";
        }

        if(this.suffix != null) {
            gen.writeString(valText + this.suffix);
        } else {
            gen.writeString(valText);
        }

    }
}